package com.meilai.project.controller.web.system;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.system.TestFlowDTO;
import com.meilai.project.service.system.testFlow.TestFlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "web端示例流程审批控制器", description = "web端示例流程审批")
@RestController
@RequestMapping("api/web/test_flow")
public class WebTestFlowController {

    @Autowired
    private TestFlowService service;

    @GetMapping("list")
    @ApiOperation(value = "列表")
    public CommonResult list(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "page_size",defaultValue = "10") Integer page_size) {
        return new CommonResult().success(service.selectPageList(page, page_size));
    }

    @PostMapping("add")
    @ApiOperation(value = "新增")
    public CommonResult add(@RequestBody TestFlowDTO dto) {
        try {
            if (service.addOne(dto)) {
                return new CommonResult().success("新增成功");
            } else {
                return new CommonResult().failed("新增失败");
            }
        }catch (Exception e){
            return new CommonResult().failed(e.getMessage());
        }
    }

    @PostMapping("edit")
    @ApiOperation(value = "修改")
    public CommonResult edit(@RequestBody TestFlowDTO dto) {
        try{
            if (service.editOne(dto)) {
                return new CommonResult().success("修改成功");
            } else {
                return new CommonResult().failed("修改失败");
            }
        }catch (Exception e){
            return new CommonResult().failed(e.getMessage());
        }
    }

    @PutMapping("check")
    @ApiOperation(value = "审核")
    public CommonResult check(@RequestBody TestFlowDTO dto) {
        try{
            if (service.check(dto)) {
                return new CommonResult().success("审核成功");
            } else {
                return new CommonResult().failed("审核失败");
            }
        }catch (Exception e){
            return new CommonResult().failed(e.getMessage());
        }
    }
}
